package com.yimidida.library.base;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.yimidida.library.R;

import pl.droidsonroids.gif.GifDrawable;

/**
 * Create on: 10/19/21
 * by Mr.Zhu
 * Description: 网络请求loading
 */
public class LoadingDialog {

    private Activity activity;
    private boolean canCancel = false;
    private PopupWindow popupWindow;
    private View rootView;
    private ImageView backImg;

    public LoadingDialog(Activity activity) {
        this.activity = activity;
    }

    public PopupWindow showLoading() {
        try {
            LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rootView = layoutInflater.inflate(R.layout.dialog_loading, null);
            backImg = rootView.findViewById(R.id.img_left);
//            if (activity instanceof CoreActivity) {
//                TitleBar titleBar = ((CoreActivity) activity).getTitleBar();
//                if (titleBar != null && titleBar.getLeftImageView() != null && titleBar.getLeftImageView().getVisibility() == View.VISIBLE) {
//                    backImg.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            dismiss();
//                            activity.finish();
//                        }
//                    });
//                }
//            }
            rootView.setFocusableInTouchMode(true); // 设置view能够接听事件
            rootView.setFocusable(true); // 设置view能够接听事件

            popupWindow = new PopupWindow(rootView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            popupWindow.setFocusable(canCancel);
            popupWindow.setOutsideTouchable(canCancel);   // 设置允许在外点击消失
            popupWindow.update();

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                rootView.setBackgroundResource(R.drawable.shape_gradient_center_black);
            }
            ImageView loadingImageView = rootView.findViewById(R.id.img_loading);
            GifDrawable drawableAnim = new GifDrawable(rootView.getResources(), R.mipmap.loading);
            drawableAnim.reset();
            drawableAnim.setSpeed(0.5f);

            loadingImageView.setImageDrawable(drawableAnim);
            drawableAnim.start();
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER | Gravity.CENTER, 0, 0);
        } catch (Exception e) {
        }

        return popupWindow;
    }

    public PopupWindow getDialog() {
        return popupWindow;
    }

    public View getRootView() {
        return rootView;
    }

    public boolean isShowing() {
        return popupWindow != null && popupWindow.isShowing();
    }

    public void dismiss() {
        if (isShowing()) {
            try {
                popupWindow.dismiss();
                popupWindow = null;
            } catch (Exception e) {
            }
        }
    }

    public boolean isCanCancel() {
        return canCancel;
    }

    public void setCanCancel(boolean canCancel) {
        this.canCancel = canCancel;
    }
}
